/**
 * @file cc.h
 * @author zdk
 * @brief 定义一些平台相关的数据类型，以及调试相关的一些宏定义
 * @version 0.1
 * @date 2021-07-07
 * 
 * BSD 3-Clause License
 * 
 * Copyright (c) 2021, water_zhang
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the copyright holder nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

#if !defined(CC_H)
#define CC_H

#include "rtthread.h"

/**
 * 定义使用到的数据类型，其实如果编译器支持C99标准，可以将LWIP_NO_STDINT_H定义为0，
 * 这也是LwIP的默认设置，这样LwIP会自动包含stdint.h这个头文件而不用自己手动定义数据类型。
 * 具体可以查阅arch.h中第110行的注释，老版本（比如1.4.1）可能会有略微不同。
 */
#define LWIP_NO_STDINT_H (1)
typedef unsigned char           u8_t;
typedef signed char             s8_t;
typedef unsigned short          u16_t;
typedef signed short            s16_t;
typedef unsigned int            u32_t;
typedef signed int              s32_t;
typedef unsigned int            mem_ptr_t;

/**
 * 定义结构体字节对齐相关的宏定义，防止结构体中间出现空隙
 * 其实如果使用的是GCC编译/clang编译器，这些宏定义也没必要定义，
 * LwIP已经把该做的做了。参考arch.h第319行。
 * 
 */
#define PACK_STRUCT_FIELD(x) x                          /* 对于GCC来说，这个没什么用 */
#define PACK_STRUCT_STRUCT      __attribute__((packed)) /* 如果是keil，用下面的宏定义 */
#define PACK_STRUCT_BEGIN                               /* 如果你用keil，那么这里定义为__pcked */
#define PACK_STRUCT_END                                 /* 对于GCC来说，这个没什么用 */

/**
 * 调试信息输出的相关宏定义，调试相关的功能配置，可以参考debug.h
 * 如果不定义这些宏，所有调试，断言，错误相关的消息会输出到printf，
 * 所以直接把printf重定向到串口也可以，这些宏的默认值可以参考arch.h
 * 的第75行往后的区域。
 * 
 */
#define LWIP_DEBUG /* LwIP调试功能的总开关，之后可以使能各模块单独的调试开关 */

/* 被LWIP_DEBUGF使用 */
#define LWIP_PLATFORM_DIAG(x) do{rt_kprintf x;}while(0) 

/* 被LWIP_ASSERT使用 */
#define LWIP_PLATFORM_ASSERT(x) do {rt_kprintf("Assertion \"%s\" failed at line %d in %s\n", \
                                     x, __LINE__, __FILE__);} while(0)

/* 输出占位符相关的宏，如果不定义，将会使用arch.h中的默认值 */
#define X8_F "02X"
#define U16_F "u"
#define S16_F "d"
#define X16_F "04X"
#define U32_F "u"
#define S32_F "d"
#define X32_F "08X"
#define SZT_F "u"

/* 设置LwIP使用的错误码提供方，摘录arch.h第376行的注释 */
/** LWIP_PROVIDE_ERRNO==1: Let lwIP provide ERRNO values and the 'errno' variable.
 * If this is disabled, cc.h must either define 'errno', include <errno.h>,
 * define LWIP_ERRNO_STDINCLUDE to get <errno.h> included or
 * define LWIP_ERRNO_INCLUDE to <errno.h> or equivalent.
 */
#define LWIP_PROVIDE_ERRNO (1)

/* 定义处理器的字节序，对于stm32使用的是小端模式，默认配置依然在arch.h中，为小端模式 */
#ifndef BYTE_ORDER
#define BYTE_ORDER LITTLE_ENDIAN
#endif

/* 移植者需要实现一个sys_now函数来获取当前的系统时基数值，这里只给出原型，实现在别处 */
extern u32_t sys_now(void);

#endif // CC_H
